package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.dto.CheckItemDTO;
import com.itheima.pojo.entity.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public void addCheckItem(CheckItemDTO checkItemDTO) {
        CheckItem checkItem = new CheckItem();
        BeanUtils.copyProperties(checkItemDTO,checkItem);
        checkItemMapper.addCheckItem(checkItem);
    }

    @Override
    public void deleteCheckItemById(String id) {
        checkItemMapper.deleteCheckItemById(Integer.valueOf(id));
    }

    @Override
    public Integer[] findCheckItemIdsByCheckGroupId(String checkgroupId) {
        Integer[] array=checkItemMapper.findCheckItemIdsByCheckGroupId(Integer.valueOf(checkgroupId));
        return array;
    }

    @Override
    public void editCheckItem(CheckItem checkItem) {
        checkItemMapper.updateCheckItem(checkItem);
    }

    @Override
    public PageResult findPageCheckItem(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<CheckItem> checkItemList =checkItemMapper.selectAllCheckItem();
        PageInfo<CheckItem> checkItemPageInfo = new PageInfo<>(checkItemList);
        return new PageResult(checkItemPageInfo.getTotal(),checkItemPageInfo.getList());
    }

    @Override
    public Object[] findAll() {
        List<CheckItem> checkItems = checkItemMapper.selectAllCheckItem();
        return checkItems.toArray(new Object[0]);
    }

    @Override
    public Object findByIdCheckItem(String id) {
        Object array=checkItemMapper.findByIdCheckItem(Integer.valueOf(id));
        return array;
    }
}
